package cn.alm.allinoneboot.controller;

import cn.alm.allinoneboot.entity.Brand;
import cn.alm.allinoneboot.entity.Product;
import cn.alm.allinoneboot.excel.ExcelWriterHandler;
import cn.alm.allinoneboot.service.BrandService;
import cn.alm.allinoneboot.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("api/export")
@CrossOrigin(origins = "*", maxAge = 3600)
@Slf4j
public class ExportController {

    @Resource
    private ExcelWriterHandler excelWriterHandler;

    @Resource
    private BrandService brandService;

    @Resource
    private ProductService productService;

    @GetMapping("excel/{entity}")
    public void export(HttpServletResponse response, @PathVariable("entity") String entity) throws IOException {
        if (entity.equals("brand")) {
            List<Brand> allBrandList = brandService.getAllBrandList();
            excelWriterHandler.writeBrand(allBrandList);
        } else if (entity.equals("product")) {
            List<Product> allProductList = productService.getAllProductList();
            excelWriterHandler.writeProduct(allProductList);
        }
    }

}
